package com.hjxr.quota.define.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.common.core.controller.BaseController;
import com.hjxr.quota.define.domain.QuotaReport;
import com.hjxr.quota.define.service.IQuotaReportService;
import com.hjxr.quota.util.PageUtil;
import com.hjxr.quota.datasource.controller.BaseForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 报表表 前端控制器
 * </p>
 *
 * @author ma
 * @since 2020-10-16
 */
@RestController
@RequestMapping("/quotaReport")
@Api(tags = "报表处理接口")
public class QuotaReportController extends BaseController {
    @Autowired
    private IQuotaReportService quotaReportService;

    @PostMapping("/upload")
    @ApiOperation("上传文件并将文件信息存入报表信息表")
    private R<String> upload(@RequestParam("filePath") MultipartFile file, @RequestParam Map<String, String> params, HttpServletRequest request) throws Exception {
        R<String> success = success(quotaReportService.fileUpload(file,request));
        String path =success.getData();
        params.put("filePath",path);
        if (success.getCode() == 0) {
            Integer num = quotaReportService.fileInformationToDb(params);//插入返回id 失败为0
            if (num == 0) {
                return failed("文件上传成功；文件信息存入表失败！");
            }
        }
        return success;
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除文件及报表信息")
    private R<Boolean> delete(@PathVariable(value = "id") Integer id) {
        return success(quotaReportService.deleteFile(id));
    }

    @GetMapping("/queryByThemeId")
    @ApiOperation("根据主题id分页查询")
    public R<PageUtil> list(@RequestParam Map<String, Object> params) {
        return success(quotaReportService.queryReportListByThemeId(params));
    }
    @GetMapping("/queryByFileName")
    @ApiOperation("根据文件名称分页查询")
    public R<PageUtil> queryByFileName(@RequestParam Map<String, Object> params) {
        return success(quotaReportService.queryReportListByFileName(params));
    }
    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("分页查询所有数据")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", dataType = "String", name = "current", value = "当前页", defaultValue = "1", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "size", value = "一页大小", defaultValue = "10", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "Boolean", name = "ifCount", value = "是否查询总数", defaultValue = "true"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "ascs", value = "升序字段，多个用逗号分隔"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "descs", value = "降序字段，多个用逗号分隔")
            })
    //TODO  不确定该接口哪里用到，但是想把current修改pageNo，size修改为pageSize
    public R<IPage<QuotaReport>> selectAll() {
        BaseForm form = new BaseForm();
        QueryWrapper<QuotaReport> query = (QueryWrapper<QuotaReport>) form.pageQueryWrapperCustom(form.getParameters(), new QueryWrapper<QuotaReport>());
        return success(quotaReportService.page(form.getPlusPagingQueryEntity(), query));
    }


    @PostMapping("/computeValue")
    @ApiOperation("ss")
    public R<Float> computeValue(@RequestBody Map<String,Object> params){
        Float f=quotaReportService.computeValue(params);
        if(f ==null){
            return failed("未查询到该值！");
        }
        return success(f);
    }
}
